package org.osami.androidui;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;

interface IAsyncImageLoaderDelegate
{
    // Called when an image is rendered
    public void onImageReceived(Drawable image);
}

class AsyncImageLoader extends Thread
{
    String url;
    IAsyncImageLoaderDelegate delegate;
    
    public AsyncImageLoader(String url, IAsyncImageLoaderDelegate delegate)
    {
        this.url = url;
        this.delegate = delegate;
        
        start();
    }
    
    public void run()
    { 
    	URL url;
		try {
			url = new URL(this.url);
			InputStream content;
			content = (InputStream)url.getContent();
			
			Drawable icon = Drawable.createFromStream(content , "src");
			this.delegate.onImageReceived(icon);
			
		}
		catch (MalformedURLException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
        //HttpURLConnection conn = (HttpURLConnection)(new URL(url)).openConnection();
        //conn.connect();
        //ImageDisplayer displayer = new ImageDisplayer(view, BitmapFactory.decodeStream(conn.getInputStream()));
        //callback.onImageReceived(displayer);
    }
}
 

/**
 * Holds information describing service.
 * 
 * Each service must define: - UI URL to (remote) web page implementing the user
 * interface. - ICON URL used on the service list. - Name of the service. -
 * Short description of the service(~20 chars).
 * 
 * @author jussi
 */
public class OsamiServiceInfo implements IAsyncImageLoaderDelegate {

	private String name = "";
	private String url_ui = "";
	private String url_icon = "";
	private String osgi_topic = "";
	private String description = "";
	private Drawable icon;
	private ImageView async_loaded_image;
	private ServicesListAdapter adapter;

	// To refresh the list once when all icons are loaded.
	private static int globalLoaderID = 0;
	private int loaderID;
	
	public OsamiServiceInfo(String name, String description) {
		this.setName(name);
		this.setDescription(description);
		
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getUiUrl() {
		return url_ui;
	}

	public void setUiUrl(String urlUi) {
		url_ui = urlUi;
	}

	public String getUrlIcon() {
		return url_icon;
	}
	
	final Runnable applyAsyncIconToImageRunnable = new Runnable() {
        public void run() {
        	
        	// Refresh list when last icon is loaded.
        	if(globalLoaderID == loaderID){
        		async_loaded_image.setImageDrawable(icon);
        		async_loaded_image.invalidate();
        	
        		adapter.notifyDataSetInvalidated();
        		adapter = null;
        	} 
        }
    };
    
    AsyncImageLoader loader;
	public void loadIconToView(ImageView im, ServicesListAdapter adapter){
		this.adapter = adapter;
		
		// Not loaded at all
		if(loader == null && icon == null){
			icon = null;
			async_loaded_image = im;
			
			globalLoaderID++;
			loaderID = globalLoaderID;
			
			loader = new AsyncImageLoader(url_icon, this);
		}
		// Still loading
		else if( loader == null){
			im.setImageDrawable(icon);
		}
		else {
			async_loaded_image = im;
		}
	}
	
	public void onImageReceived(Drawable image) { 
		this.icon = image;
		OsamiUI.getInstance().runOnUiThread(applyAsyncIconToImageRunnable);
		this.loader = null;
		
	}
	 
	public void setUrlIcon(String urlIcon) {
		icon = null;
		url_icon = urlIcon;
	}
	
	public Drawable getIcon() {
		return icon;
	}

	public String getOsgiTopic() {
		return osgi_topic;
	}

	public void setOsgiTopic(String osgiTopic) {
		osgi_topic = osgiTopic;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}
